package com.example.androidlearning2200;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.viewpager.widget.ViewPager;

import com.example.androidlearning2200.adapter.WelcomeAdapter;

import java.util.ArrayList;
import java.util.List;

import me.relex.circleindicator.CircleIndicator;

public class WelcomeActivity extends BaseActivity {

    private ViewPager vpWelcome;
    private CircleIndicator indicator;
    private List<Integer> datas = new ArrayList<>();
    private Button btnStart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        // 找到ViewPager控件
        vpWelcome = findViewById(R.id.vpWelcome);
        // 找到CircleIndicator圆点指示器
        indicator = findViewById(R.id.indicator);
        btnStart = findViewById(R.id.btnStart);
        // 准备要显示的图片数据
        datas.add(R.mipmap.welcome1);
        datas.add(R.mipmap.welcome2);
        datas.add(R.mipmap.welcome3);
        datas.add(R.mipmap.welcome4);
        datas.add(R.mipmap.welcome5);
        // 设置适配器
        vpWelcome.setAdapter(new WelcomeAdapter(this, datas));
        // 设置滑动监听
        vpWelcome.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                // 最后一页（position==4）显示开始使用按钮，其他页不显示
                btnStart.setVisibility(position == 4 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        // CircleIndicator指示器绑定ViewPager
        indicator.setViewPager(vpWelcome);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sharedPreferences = getSharedPreferences("AppConfig", MODE_PRIVATE);
                sharedPreferences.edit().putBoolean("is_first",false).commit();
                startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
            }
        });
    }

}